﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using VideoClassLibrary.Extension;

namespace VideoClassLibrary.BusinessLogicLayer
{
    public class CourseWeek
    {
        public string GetCourseWeek(int currYear, int currMonth, int startDay, int type, ref DateTime minTime, ref DateTime maxTime,DataSet dataSet)
        {
            StringBuilder headTable = new StringBuilder();
            DateTime dtDate = new DateTime(currYear, currMonth, startDay);

           // DataSet dataSet = CommonCacheManager.GetCacheData<DataSet>("UserCourse" + MembershipService.GetUser().ID);

            //dv.Table = dataSet.Tables[0];
            //dv.Sort = "CreateTime desc";
            //GridView.DataSource = dv;



            switch (dtDate.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    WeekList(dtDate, headTable, dataSet, 1);
                    break;

                case DayOfWeek.Tuesday:
                    WeekList(dtDate, headTable, dataSet, 2);
                    break;
                case DayOfWeek.Wednesday:
                    WeekList(dtDate, headTable, dataSet, 3);
                    break;
                case DayOfWeek.Thursday:
                    WeekList(dtDate, headTable, dataSet, 4);
                    break;
                case DayOfWeek.Friday:
                    WeekList(dtDate, headTable, dataSet, 5);
                    break;
                case DayOfWeek.Saturday:
                    WeekList(dtDate, headTable, dataSet, 6);
                    break;
                case DayOfWeek.Sunday:
                    WeekList(dtDate, headTable, dataSet, 7);
                    break;

            }




            minTime = dtDate.AddDays(-7);
            maxTime = dtDate.AddDays(7);
            return headTable.ToString();
        }

        private void WeekList(DateTime dtDate, StringBuilder headTable, DataSet dataSet, int addday)
        {
            string playJs = string.Empty;
            string courseInfo = string.Empty;

            headTable.Append("<ul class='thead'><li class='course_type'></li><li class='course_up course_up1'>上午</li><li class='course_down course_down1'>下午</li></ul>");
            for (int i = 1; i < 8; i++)
            {
                headTable.AppendFormat(" <ul  class='thead'><li> {0}/{1}</li>", ConvertNumToCN(dtDate.AddDays(i - addday).DayOfWeek),
                                       dtDate.AddDays(i - addday).ToString("MM月dd日"));
                StringBuilder sbup = new StringBuilder();
                StringBuilder down = new StringBuilder();
                if (dataSet != null)
                {
                    foreach (DataRow dr in dataSet.Tables[0].Rows)
                    {
                        playJs = string.Format("VideoPlay('{0}','{1}','{2}','{3}','{4}')", dr["ID"],
                                               DateTime.Parse(dr["VCL_startDate"].ToString()).
                                                   ToShortDateString(),
                                               dr["RoomID"],
                                               dr["VCL_startTime"], dr["VCL_EndTime"]);
                        courseInfo = "课程：" + dr["VC_name"].ToString() + "   时间：" +
                                     DateTime.Parse(dr["VCL_startTime"].ToString()).ToString("HH:mm") + "-" +
                                     DateTime.Parse(dr["VCL_EndTime"].ToString()).ToString("HH:mm") + " 教室：" +
                                     dr["VR_roomName"].ToString();

                        if (dr["VCL_startDate"].ToString() == dtDate.AddDays(i - addday).ToString())
                        {
                            if (DateTime.Parse(dr["VCL_startTime"].ToString()).Hour < 12)
                            {
                                sbup.AppendFormat(
                                    "<a href='javascript:' style=\"cursor:hand\" onclick=\"" +
                                    playJs +
                                    "\" onmouseover=\"this.title ='" + courseInfo + "'\">" +
                                    StringUtils.getLimitLengthString(dr["VC_name"].ToString(), 8, "") +
                                    "</a> ");
                            }
                            else
                            {
                                down.AppendFormat(
                                    "<a href='javascript:' style=\"cursor:hand\" onclick=\"" +
                                    playJs +
                                    "\" onmouseover=\"this.title ='" + courseInfo + "'\">" +
                                    StringUtils.getLimitLengthString(dr["VC_name"].ToString(), 8, "") +
                                    "</a>");
                            }
                        }

                    }


                }
                headTable.AppendFormat("<li class='course_up'>{0}</li>", sbup.ToString());
                headTable.AppendFormat("<li class='course_down'>{0}</li>", down.ToString());
                headTable.Append("</ul>");
            }
        }

        private string GetDay(int i, DateTime dateTime)
        {
            DayOfWeek dayOfWeek = (DayOfWeek)i;
            if (dayOfWeek == dateTime.DayOfWeek)
            {
                return dateTime.ToString("MM月dd日");

            }
            else
            {
                return "";
            }


        }

        private string ConvertNumToCN(int i)
        {
            string Cn = string.Empty;
            switch (i)
            {
                case 1:
                    Cn = "一";
                    break;
                case 2:
                    Cn = "二";
                    break;

                case 3:
                    Cn = "三";
                    break;
                case 4:
                    Cn = "四";
                    break;
                case 5:
                    Cn = "五";
                    break;
                case 6:
                    Cn = "<font color=red><strong>六</strong></font>";
                    break;
                case 7:
                    Cn = "<font color=red><strong>日</strong></font>";
                    break;
            }
            return Cn;
        }
        private string ConvertNumToCN(DayOfWeek dayOfWeek)
        {
            string Cn = string.Empty;
            switch (dayOfWeek)
            {
                case DayOfWeek.Monday:
                    Cn = "一";
                    break;
                case DayOfWeek.Tuesday:
                    Cn = "二";
                    break;

                case DayOfWeek.Wednesday:
                    Cn = "三";
                    break;
                case DayOfWeek.Thursday:
                    Cn = "四";
                    break;
                case DayOfWeek.Friday:
                    Cn = "五";
                    break;
                case DayOfWeek.Saturday:
                    Cn = "<font color=red><strong>六</strong></font>";
                    break;
                case DayOfWeek.Sunday:
                    Cn = "<font color=red><strong>日</strong></font>";
                    break;
            }
            return Cn;
        }

    }
}

